package com.yipei.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yipei.business.domain.MlOptionalSymbol;
import com.yipei.business.service.IMlOptionalSymbolService;
import com.yipei.business.vo.MlKLineMasterVo;
import com.yipei.business.vo.OptionalSymbolVo;
import com.yipei.common.annotation.Log;
import com.yipei.common.core.controller.BaseController;
import com.yipei.common.core.domain.AjaxResult;
import com.yipei.common.core.page.TableDataInfo;
import com.yipei.common.enums.BusinessType;
import com.yipei.common.utils.SecurityUtils;
import com.yipei.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 我的自选Controller
 *
 * @author yipei
 * @date 2024-12-21
 */
@RestController
@RequestMapping("/business/optionalsymbol")
@Api(tags = "我的自选", description = "我的自选管理")
public class MlOptionalSymbolController extends BaseController {
    @Autowired
    private IMlOptionalSymbolService mlOptionalSymbolService;

    /**
     * 查询我的自选列表
     */
    //@PreAuthorize("@ss.hasPermi('business:optionalsymbol:list')")

    @GetMapping("/list")
    public TableDataInfo list(MlOptionalSymbol mlOptionalSymbol) {
        startPage();
        List<MlOptionalSymbol> list = mlOptionalSymbolService.selectMlOptionalSymbolList(mlOptionalSymbol);
        return getDataTable(list);
    }

    @GetMapping("/optionalList")
    @ApiOperation("我的自选数据")
    public AjaxResult optionalList(OptionalSymbolVo optionalSymbolVo) throws IOException {
        String tdAcc = SecurityUtils.getLoginMember().getMlTradeAccount().getTdAcc();
        LambdaQueryWrapper<MlOptionalSymbol> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MlOptionalSymbol::getAtAcc, tdAcc);
        List<OptionalSymbolVo> optionalSymbolVoList=new ArrayList<>();
        List<MlOptionalSymbol> list = mlOptionalSymbolService.list(lambdaQueryWrapper);
        for (MlOptionalSymbol optionalSymbol : list) {
            String symbol = optionalSymbol.getSymbol();
            OptionalSymbolVo optionalSymbolVo1=new OptionalSymbolVo();
            List<OptionalSymbolVo> optionalList = mlOptionalSymbolService.createOptionalList(symbol,optionalSymbolVo1);
            optionalSymbolVoList.addAll(optionalList);
        }
        return AjaxResult.success(optionalSymbolVoList);
    }


    /**
     * 导出我的自选列表
     */
    //@PreAuthorize("@ss.hasPermi('business:optionalsymbol:export')")
    @ApiOperation("我的自选导出数据")
    @Log(title = "我的自选", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MlOptionalSymbol mlOptionalSymbol) {
        List<MlOptionalSymbol> list = mlOptionalSymbolService.selectMlOptionalSymbolList(mlOptionalSymbol);
        ExcelUtil<MlOptionalSymbol> util = new ExcelUtil<MlOptionalSymbol>(MlOptionalSymbol.class);
        util.exportExcel(response, list, "我的自选数据");
    }

    /**
     * 获取我的自选详细信息
     */
    //@PreAuthorize("@ss.hasPermi('business:optionalsymbol:query')")
    @ApiOperation("我的自选通过ID获取数据")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(mlOptionalSymbolService.selectMlOptionalSymbolById(id));
    }

    /**
     * 新增我的自选
     */
    //@PreAuthorize("@ss.hasPermi('business:optionalsymbol:add')")
    @ApiOperation("我的自选新增数据")
//    @Log(title = "我的自选", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResult add(@RequestBody MlKLineMasterVo mlKLineMasterVo) throws Exception {
        String f12 = mlKLineMasterVo.getF12();
        String tdAcc = SecurityUtils.getLoginMember().getMlTradeAccount().getTdAcc();
        LambdaQueryWrapper<MlOptionalSymbol> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MlOptionalSymbol::getSymbol, f12);
        lambdaQueryWrapper.eq(MlOptionalSymbol::getAtAcc, tdAcc);
        if (!Objects.isNull(mlOptionalSymbolService.getOne(lambdaQueryWrapper))){
            throw new Exception("此股票已在自选股票中！");
        }
        MlOptionalSymbol mlOptionalSymbol=new MlOptionalSymbol();
        mlOptionalSymbol.setSymbol(f12);
        mlOptionalSymbol.setAtAcc(tdAcc);
        mlOptionalSymbolService.insertMlOptionalSymbol(mlOptionalSymbol);
        return AjaxResult.success();
    }

    /**
     * 修改我的自选
     */
    //@PreAuthorize("@ss.hasPermi('business:optionalsymbol:edit')")
    @ApiOperation("我的自选修改数据")
    @Log(title = "我的自选", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MlOptionalSymbol mlOptionalSymbol) {
        return toAjax(mlOptionalSymbolService.updateMlOptionalSymbol(mlOptionalSymbol));
    }

    /**
     * 删除我的自选
     */
    //@PreAuthorize("@ss.hasPermi('business:optionalsymbol:remove')")
    @ApiOperation("批量删除我的自选删除数据")
    @Log(title = "批量删除我的自选删除数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(mlOptionalSymbolService.deleteMlOptionalSymbolByIds(ids));
    }


    @ApiOperation("我的自选删除数据")
//    @Log(title = "我的自选", businessType = BusinessType.DELETE)
    @PostMapping("/deleted")
    public AjaxResult removeId( @RequestBody MlOptionalSymbol mlOptionalSymbol) {
        String symbol = mlOptionalSymbol.getSymbol();
        LambdaQueryWrapper<MlOptionalSymbol> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MlOptionalSymbol::getSymbol,symbol).eq(MlOptionalSymbol::getAtAcc,SecurityUtils.getLoginMember().getMlTradeAccount().getTdAcc());
        return toAjax(mlOptionalSymbolService.deleteMlOptionalSymbolById(Long.valueOf(mlOptionalSymbolService.getOne(lambdaQueryWrapper).getId())));
    }
}
